﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaskManagerApp.Model
{

    /// <summary>
    /// Task repository which contains list of tasks
    /// This can be any repository
    /// File based or Database or any other source which implements ITaskRepository
    /// </summary>
    public class TaskRepository : ITaskRepository
    {
        #region Fields

        readonly List<ITask> tasks = new List<ITask>();

        #endregion // Fields

        public TaskRepository()
        {
            //create sample tasks for now
            ITask task1 = Task.CreateTask();
            task1.Description = "Test Desc 1";
            task1.Start = DateTime.Now;
            task1.End = task1.Start.AddMinutes(30);
            task1.TaskType = Strings.TaskViewModel_TaskType_Business;
            tasks.Add(task1);

            ITask task2 = Task.CreateTask();
            task2.Description = "Test Desc 2";
            task2.Start = DateTime.Now.AddMinutes(60);
            task2.End = task2.Start.AddMinutes(30);
            task2.TaskType = Strings.TaskViewModel_TaskType_Business;
            tasks.Add(task2);
        }

        #region Implementation of ITaskRepository

        public event EventHandler<TaskEventArgs> taskAdded;
        public event EventHandler<TaskEventArgs> taskRemoved;

        public void AddTask(ITask task)
        {
            if (task == null)
                throw new ArgumentNullException("task");

            if (!tasks.Contains(task))
            {
                tasks.Add(task);

                if (taskAdded != null)
                    taskAdded(this, new TaskEventArgs(task)); 
            }

        }

        public void RemoveTask(ITask task)
        {
            if(task == null)
                throw new ArgumentNullException("task");

            if(tasks.Contains(task))
            {
                tasks.Remove(task);

                if(taskRemoved != null)
                    taskRemoved(this, new TaskEventArgs(task));
            }
        }

        public bool ContainsTask(ITask task)
        {
            if (task == null)
                throw new ArgumentNullException("task");

            return tasks.Contains(task);
        }

        public List<ITask> GetTasks()
        {
            return new List<ITask>(tasks);
        }

        #endregion
    }
}
